﻿using System.Net;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;

namespace PLCSharp.CoreModel.Tools
{
    public static class NetTool
    {
        private static readonly Ping Ping = new();
        public static async Task<bool> PingIPAsync(string ip)
        {
            var isip = IPAddress.TryParse(ip, out var ipAddress);
            if (isip == false)
            {
                return false;
            }
            bool isMatch = Regex.IsMatch(ip, @"^([1-9]\d?|1\d{2}|2[01]\d|22[0-3])(\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])){3}$");
            if (isMatch == false)
            {
                return false;
            }
            try
            {
                var result = await Ping.SendPingAsync(ipAddress);
                return result.Status == IPStatus.Success;
            }
            catch
            {

                return false;
            }
        }


        public static bool PingIP(string ip)
        {
            lock (Ping)
            {


                var isip = IPAddress.TryParse(ip, out var ipAddress);
                if (isip == false)
                {
                    return false;
                }
                bool isMatch = Regex.IsMatch(ip, @"^([1-9]\d?|1\d{2}|2[01]\d|22[0-3])(\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])){3}$");
                if (isMatch == false)
                {
                    return false;
                }
                try
                {

                    var result = Ping.Send(ipAddress);
                    return result.Status == IPStatus.Success;
                }
                catch
                {

                    return false;
                }
            }
        }
    }
}
